package com.palmshop.online.controller;

import java.io.IOException;

import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.palmshop.online.entity.PalmResult;
import com.palmshop.online.entity.ShoppingCart;
import com.palmshop.online.service.PayService;

/**
 * 用户支付相关功能的controller
 * @author quan
 *
 */
@Controller
@RequestMapping("/payOnline")
public class PayContorller {

	@Resource
	private PayService service;//service注入
	
	/**
	 * 支付宝支付方法入口
	 * @param request
	 * @param response
	 * @param payMoney:需要支付的金额
	 * @param saleListAddress：收货地址
	 * @param cusUnique：用户唯一性标识
	 * @param shopUnique：商铺唯一性标识
	 * @param saleListUnique：订单唯一性标识
	 * @param shoppingCartId：购物车id
	 * @param goodsCount:商品的数量
	 * @param goodsBarcodes：商品的条码
	 * @throws IOException
	 */
	@RequestMapping("/pay.do")
	@ResponseBody
	public void addBasket(HttpServletRequest request,
			HttpServletResponse response,Double payMoney,String saleListAddress,String cusUnique,String shopUnique,
			String saleListUnique,String shoppingCartId,String goodsCount,String goodsBarcodes) throws IOException{
		
		String saleListAddr = request.getParameter("saleListAddress");
		saleListAddr = new String(saleListAddr.getBytes("iso-8859-1"), "utf-8");
		
		response.setContentType("text/html;charset=utf-8");
		String sHtmlText = service.addBasket(payMoney, saleListAddress, cusUnique, shopUnique, saleListUnique, shoppingCartId,goodsCount,goodsBarcodes);
		PrintWriter out = response.getWriter();
		
		out.print(sHtmlText);
	}
		
	/**
	 * 微信支付方法入口
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/wechatPay.do")
	@ResponseBody
	public void wecahatPay(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
//		String saleListAddr = request.getParameter("saleListAddress");
//		saleListAddr = new String(saleListAddr.getBytes("iso-8859-1"), "utf-8");
		System.err.println("------------------------------------");
		
		
		ShoppingCart basket=new ShoppingCart();
		response.setContentType("text/html;charset=utf-8");
		String sHtmlText = service.wechatPay();
		PrintWriter out = response.getWriter();
		System.err.println("========" + sHtmlText);
		out.print(sHtmlText);
	}
	
	/**
	 * 货到付款方法入口
	 * @param payMoney：需要支付的金额
	 * @param saleListAddress：收货地址
	 * @param cusUnique：用户唯一性标识
	 * @param shopUnique：店铺唯一性标识
	 * @param saleListUnique：订单唯一性标识
	 * @param shoppingCartId：购物车id
	 * @param goodsCount:商品数量
	 * @param goodsBarcodes：商品条码
	 * @return json
	 * @throws IOException
	 */
	@RequestMapping("/cashPay.do")
	@ResponseBody
	public PalmResult cashPay(Double payMoney,String saleListAddress,String cusUnique,String shopUnique,
					String saleListUnique,String shoppingCartId,String goodsCount,String goodsBarcodes) throws IOException{
		
		return service.cashPay(payMoney, saleListAddress, cusUnique, shopUnique, saleListUnique, shoppingCartId, goodsCount, goodsBarcodes);
	}
	
	
	
}
